/*
 * Alberto Ruiz-Larrea Guillén...11383
 * Erdi Dogruel..................14955
 * Marcos Pérez Martín...........10324
 */

package com.example.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.AsyncTask;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

//This class sorts and shows the leaderboard
public class ShowLeaderboard extends AsyncTask<OpenDB, Integer, String[]> {

	private TableLayout text;
	String[] names;
	int[][] table;
	int team_size;
	Context c;

	public ShowLeaderboard(TableLayout t, Context context) {
		this.text = t;
		this.c = context;
	}

	@Override
	protected String[] doInBackground(OpenDB... params) {

		SQLiteDatabase ver = params[0].getReadableDatabase();
		String[] columns = { OpenDB.id, OpenDB.name, OpenDB.games_played,
				OpenDB.won_matches, OpenDB.draw_matches, OpenDB.lost_matches,
				OpenDB.score_home, OpenDB.score_away, OpenDB.points,
				OpenDB.rating };
		Cursor cursor = ver.query(OpenDB.table4, columns, null, null, null,
				null, null);
		StringBuffer res;
		cursor.moveToFirst();

		team_size = 0;
		while (!cursor.isAfterLast()) {
			team_size++;
			cursor.moveToNext();
		}
		cursor.moveToFirst();

		names = new String[team_size];
		table = new int[team_size][7];
		int i = 0;

		while (!cursor.isAfterLast()) {
			names[i] = cursor.getString(1);
			for (int z = 0; z < 7; z++)
				table[i][z] = cursor.getInt(z + 2);
			i++;
			cursor.moveToNext();
		}
		cursor.close();

		names = doSelectionSort(table, names);
		return names;

	}

	@Override
	protected void onPostExecute(String[] result) {

		TableRow first_row = new TableRow(c);
		TextView pos = new TextView(c);
		pos.setText(" Pos ");
		pos.setTextColor(Color.WHITE);
		pos.setGravity(Gravity.CENTER);
		first_row.addView(pos);
		TextView name = new TextView(c);
		name.setText(" Name ");
		name.setTextColor(Color.WHITE);
		name.setGravity(Gravity.CENTER);
		first_row.addView(name);
		TextView games_played = new TextView(c);
		games_played.setText(" GP ");
		games_played.setTextColor(Color.WHITE);
		games_played.setGravity(Gravity.CENTER);
		first_row.addView(games_played);
		TextView games_win = new TextView(c);
		games_win.setText(" W ");
		games_win.setTextColor(Color.WHITE);
		games_win.setGravity(Gravity.CENTER);
		first_row.addView(games_win);
		TextView games_draw = new TextView(c);
		games_draw.setText(" D ");
		games_draw.setTextColor(Color.WHITE);
		games_draw.setGravity(Gravity.CENTER);
		first_row.addView(games_draw);
		TextView games_lose = new TextView(c);
		games_lose.setText(" L ");
		games_lose.setTextColor(Color.WHITE);
		games_lose.setGravity(Gravity.CENTER);
		first_row.addView(games_lose);
		TextView score_goal = new TextView(c);
		score_goal.setText(" G ");
		score_goal.setTextColor(Color.WHITE);
		score_goal.setGravity(Gravity.CENTER);
		first_row.addView(score_goal);
		TextView get_goal = new TextView(c);
		get_goal.setText(" CG ");
		get_goal.setTextColor(Color.WHITE);
		get_goal.setGravity(Gravity.CENTER);
		first_row.addView(get_goal);
		TextView points = new TextView(c);
		points.setText(" P ");
		points.setTextColor(Color.WHITE);
		points.setGravity(Gravity.CENTER);
		first_row.addView(points);
		text.addView(first_row);

		for (int i = 0; i < team_size; i++) {
			TableRow tbrow = new TableRow(c);
			TextView t1v = new TextView(c);
			t1v.setText(new Integer(i + 1).toString());
			t1v.setTextColor(Color.WHITE);
			t1v.setGravity(Gravity.CENTER);
			tbrow.addView(t1v);
			TextView t2v = new TextView(c);
			t2v.setText(names[i]);
			t2v.setTextColor(Color.WHITE);
			t2v.setGravity(Gravity.LEFT);
			tbrow.addView(t2v);
			TextView t3v = new TextView(c);
			t3v.setText(new Integer(table[i][0]).toString());
			t3v.setTextColor(Color.WHITE);
			t3v.setGravity(Gravity.CENTER);
			tbrow.addView(t3v);
			TextView t4v = new TextView(c);
			t4v.setText(new Integer(table[i][1]).toString());
			t4v.setTextColor(Color.WHITE);
			t4v.setGravity(Gravity.CENTER);
			tbrow.addView(t4v);
			TextView t5v = new TextView(c);
			t5v.setText(new Integer(table[i][2]).toString());
			t5v.setTextColor(Color.WHITE);
			t5v.setGravity(Gravity.CENTER);
			tbrow.addView(t5v);
			TextView t6v = new TextView(c);
			t6v.setText(new Integer(table[i][3]).toString());
			t6v.setTextColor(Color.WHITE);
			t6v.setGravity(Gravity.CENTER);
			tbrow.addView(t6v);
			TextView t7v = new TextView(c);
			t7v.setText(new Integer(table[i][4]).toString());
			t7v.setTextColor(Color.WHITE);
			t7v.setGravity(Gravity.CENTER);
			tbrow.addView(t7v);
			TextView t8v = new TextView(c);
			t8v.setText(new Integer(table[i][5]).toString());
			t8v.setTextColor(Color.WHITE);
			t8v.setGravity(Gravity.CENTER);
			tbrow.addView(t8v);
			TextView t9v = new TextView(c);
			t9v.setText(new Integer(table[i][6]).toString());
			t9v.setTextColor(Color.WHITE);
			t9v.setGravity(Gravity.CENTER);
			tbrow.addView(t9v);
			text.addView(tbrow);
		}
	}

	public String[] doSelectionSort(int[][] arr, String[] str) {

		for (int i = 0; i < arr.length - 1; i++) {
			int index = i;
			for (int j = i + 1; j < team_size; j++)
				if ((arr[j][6] > arr[index][6])
						|| ((arr[j][6] == arr[index][6]) && ((arr[j][4] - arr[j][5]) > (arr[index][4] - arr[index][5])))
						|| ((arr[j][6] == arr[index][6])
								&& ((arr[j][4] - arr[j][5]) == (arr[index][4] - arr[index][5])) && (arr[j][4] > arr[index][4])))
					index = j;

			for (int z = 0; z < 7; z++) {
				int smallerNumber = arr[index][z];
				arr[index][z] = arr[i][z];
				arr[i][z] = smallerNumber;
			}

			String smallString = str[index];
			str[index] = str[i];
			str[i] = smallString;
		}
		return str;
	}
}